package com.up360.rapidcalc.aerolite.scene
{
    import com.up360.rapidcalc.aerolite.AeroliteCrisis;
    import com.up360.rapidcalc.aerolite.model.Form;
    import com.up360.rapidcalc.aerolite.model.Subject;
    import com.up360.rapidcalc.aerolite.swc.FailureUI;
    import com.up360.rapidcalc.aerolite.swc.SuccessUI;
    import com.up360.rapidcalc.aerolite.util.TextView;
    import com.up360.rapidcalc.common.rankinglist.RankingList;
    
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.ColorTransform;
    import flash.geom.Transform;
    import flash.net.URLVariables;
    import flash.text.TextField;
    import flash.text.TextFormat;

    /**
     * @author Tunie
     * @date   May 8, 2013
     */
    public class PromptPage extends Page
    {
        protected var _ui:MovieClip;
        protected var _form:Form;
        protected var _isSuccess:Boolean;
        private var _ranking:RankingList;
        
        public function PromptPage(form:Form,isSuccess:Boolean)
        {
            super();
            _form=form;
            _isSuccess=isSuccess;
            //
            configUI();
            addChild(_ui);
            init();
            _ui.addEventListener(MouseEvent.CLICK,uiClick);
        }
        
        private function configUI():void
        {
            if(_isSuccess) _ui=new SuccessUI();
            else _ui=new FailureUI();
        }
        
        private function uiClick(e:MouseEvent):void
        {
            var b_name:String=e.target.name;
            switch(b_name)
            {
                case "btnRestart":
                {
                    AeroliteCrisis.instance.start();
                    break;
                }
                    
                default:
                {
                    break;
                }
            }
        }
        
        override public function disruption():void
        {
            // TODO Auto Generated method stub
            super.disruption();
        }
        
        override public function init():void
        {
            super.init();
            //
            var b_ui:MovieClip=_ui as MovieClip;
            b_ui.mc.txtLevel.text="第 "+_form.contCpId+" 关";
            b_ui.mc.txtUseTime.text=_form.consTime+" 秒";
            b_ui.mc.txtTotalCount.text=_form.rightCount+" 题";
            b_ui.mc.txtRightCount.text=_form.rightCount+" 题";
            b_ui.mc.txtErrorCount.text=_form.wrongCount+" 题";
            //设置各文本样式
            configElseTf(b_ui.mc.txtUseTime);
            configElseTf(b_ui.mc.txtTotalCount);
            configElseTf(b_ui.mc.txtRightCount);
            configElseTf(b_ui.mc.txtErrorCount);
            configLevelTf(b_ui.mc.txtLevel);
            //显示错题列表
//            trace("显示错题：");
//            _form.errorSubject.display();
            //
            changeErrorBoardColor();
            var b_textView:TextView=new TextView(0,0,b_ui.mc.mcErrorBoard);
            var b_size:int=_form.errorSubject.size();
            if(b_size>0)
            {
                for(var i:int=0;i<b_size;i++)
                {
                    b_textView.appandText((_form.errorSubject.get(i) as Subject).getSubject());
                }
            }
            //为重玩游戏预先加载试题
            AeroliteCrisis.instance.restart();
        }
        
        private function changeErrorBoardColor():void
        {
            var b_transColor:ColorTransform=new ColorTransform();
            if(_isSuccess)
            {
                b_transColor.redOffset=0x10;
                b_transColor.greenOffset=0x96;
                b_transColor.blueOffset=0x61;
            }
            else
            {
                b_transColor.redOffset=0x5d;
                b_transColor.greenOffset=0x5e;
                b_transColor.blueOffset=0x60;
            }
            var b_eb:MovieClip=_ui.mc.mcErrorBoard;
            b_eb.mcBorder.transform.colorTransform=b_transColor;
            b_eb.mcBar.transform.colorTransform=b_transColor;
        }
        
        /**
         *更新排行榜 
         * @param url 取得学生的信息接口
         * @param vals 取得学生的信息接口上传的参数
         */
        public function updateRanking(url:String,vals:URLVariables):void
        {
            var b_colorType:int=RankingList.FAILURE_COLOR;
            if(_isSuccess) b_colorType=RankingList.SUCCESS_COLOR;
            _ranking=new RankingList(url,vals,b_colorType);
            _ranking.x=11;
            _ranking.y=202;
            _ui.mc.addChild(_ranking);
        }
        
        private function configLevelTf(param0:TextField):void
        {
            var b_format:TextFormat=param0.defaultTextFormat;
            var b_len:int=param0.text.length;
            b_format.color=0x0a410f;
            param0.setTextFormat(b_format,0,1);
            param0.setTextFormat(b_format,b_len-1,b_len);
        }
        
        private function configElseTf(param0:TextField):void
        {
            var b_format:TextFormat=param0.defaultTextFormat;
            var b_len:int=param0.text.length;
            b_format.color=0x0a410f;
            param0.setTextFormat(b_format,b_len-1,b_len);
        }
    }
}